<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *消费排行
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'listd';
$merchid=$this->checklogin();
$tabs=array(
    array('op'=>'listd','title'=>'日消费排行'),
    array('op'=>'listm','title'=>'月消费排行'),
    array('op'=>'listy','title'=>'年消费排行'),
    array('op'=>'listz','title'=>'总消费排行（计算量大会卡顿）'),
);
$y=$_RQ['y']?:date('Y');
$m=$_RQ['m']?:date('m');
$d=$_RQ['d']?:date('d');
$return['params']['y']=$y;
$return['params']['m']=intval($m).'';
$return['params']['d']=intval($d).'';
if($_RQ['op']=='listd'){
    $pmrq=$y.'.'.$m.'.'.$d;
    $d2=$d+1;
    $starttime=strtotime($y.'-'.$m.'-'.$d);
    $endtime=strtotime($y.'-'.$m.'-'.$d2);
    $search=array(
        array('name'=>'y','width'=>15,'title'=>'年','type'=>'radio','options'=>'ys'),
        array('name'=>'m','width'=>15,'title'=>'月','type'=>'radio','options'=>'months'),
        array('name'=>'d','width'=>15,'title'=>'日','type'=>'radio','options'=>'days'),
    );
}
if($_RQ['op']=='listm'){
    $pmrq=$y.'.'.$m;
    $m2=$m+1;
    $starttime=strtotime($y.'-'.$m.'-1');
    $endtime=strtotime($y.'-'.$m2.'-1');
    $search=array(
        array('name'=>'y','width'=>15,'title'=>'年','type'=>'radio','options'=>'ys'),
        array('name'=>'m','width'=>15,'title'=>'月','type'=>'radio','options'=>'months'),
    );
}
if($_RQ['op']=='listy'){
    $pmrq=$y;
    $y2=$y+1;
    $starttime=strtotime($y.'-1-1');
    $endtime=strtotime($y2.'-1-1');
    $search=array(
        array('name'=>'y','width'=>15,'title'=>'年','type'=>'radio','options'=>'ys'),
    );
}

$params=' and createtime>'.$starttime.' and createtime<'.$endtime;

if($_RQ['op']=='listz'){
    $params='';
    $search='';
}

$return['list']=pdo_fetchall('SELECT mid,SUM(price),count(1) FROM '.tablename('jy_weishop_order').' WHERE ispay=1 and merchid= '.$merchid.$params.' group by mid');
foreach($return['list'] as $k=>&$v){
    $member=pdo_get('core_members',array('id'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','nickname','mobile'));
    $member2=pdo_get('jy_weishop_member',array('merchid'=>$merchid,'mid'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),'level');
    $member3=pdo_get('jy_weishop_member_level',array('merchid'=>$merchid,'id'=>$member2['level']),'title');
    if(!$member['id']){
        unset($return['list'][$k]);
        continue;
    }
    $v['nickid']=$v['mid'];
    if($member['nickname']){
        $v['nickname']=$member['nickname'];
    }else{
        $v['nickname']='散客用户'.$v['mid'].'(未授权)';
    }
    
    $v['level']=$member3['title'];
    $v['mobile']=$member['mobile'];
    $v['total']=$v['count(1)'];
    $v['totalprice']=$v['SUM(price)'];
}
paixu($return['list'],'SUM(price)','desc');
foreach($return['list'] as $k=>&$v){
    $v['rank']=$k+1;
}
$htmls=array(
    'type'=>'list',
    'tabs'=>$tabs,
    'search'=>$search,
    'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
        array('name'=>'rank','title'=>$pmrq.' 排名','type'=>'string'),
        array('name'=>'nickid','title'=>'会员ID','type'=>'string'),
        array('name'=>'nickname','title'=>'会员昵称','type'=>'string'),
        array('name'=>'level','title'=>'商城等级','type'=>'string'),
        array('name'=>'mobile','title'=>'手机','type'=>'string'),         
        array('name'=>'totalprice','title'=>'消费金额','type'=>'string'),
        array('name'=>'total','title'=>'订单数','type'=>'string'),
    )),
);
$return['htmls']=$htmls;
$nowy=date('Y');
$y1=$nowy-2;
$y2=$nowy-1;
$return['ys']=array(
    $y1=>$y1.'年',
    $y2=>$y2.'年',
    $nowy=>$nowy.'年' ,
);
for($i=1;$i<=31;$i++){
    $days[$i]=$i.'日';
    if($i<=12){$months[$i]=$i.'月';}
}
$return['months']=$months;
$return['days']=$days;
exi($return);